﻿/**  版本信息模板在安装目录下，可自行修改。
* Supplier.cs
*
* 功 能： N/A
* 类 名： Supplier
*
* Ver    变更日期             负责人  变更内容
* ───────────────────────────────────
* V0.01  2013-4-22 16:49:36   N/A    初版
*
* Copyright (c) 2012 Maticsoft Corporation. All rights reserved.
*┌──────────────────────────────────┐
*│　此技术信息为本公司机密信息，未经本公司书面同意禁止向第三方披露．　│
*│　版权所有：动软卓越（北京）科技有限公司　　　　　　　　　　　　　　│
*└──────────────────────────────────┘
*/
using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;

namespace DMZS.DAL
{
    /// <summary>
    /// 数据访问类:Supplier
    /// </summary>
    public partial class Supplier
    {
        public Supplier()
        {
        }

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("ID", "Supplier");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID)
        {
            var strSql = new StringBuilder();
            strSql.Append("select count(1) from Supplier");
            strSql.Append(" where ID=@ID");
            var parameters = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4)
            };
            parameters[0].Value = ID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(DMZS.Model.Supplier model)
        {
            var strSql = new StringBuilder();
            strSql.Append("insert into Supplier(");
            strSql.Append("Code,Name,NamePY,FullName,Address,Email,PostCode,Fax,Linkman,LinkPhone,Linkman1,LinkPhone1,Leveal,Remark,IsStop,AreaID)");
            strSql.Append(" values (");
            strSql.Append("@Code,@Name,@NamePY,@FullName,@Address,@Email,@PostCode,@Fax,@Linkman,@LinkPhone,@Linkman1,@LinkPhone1,@Leveal,@Remark,@IsStop,@AreaID)");
            strSql.Append(";select @@IDENTITY");
            var parameters = new SqlParameter[] { new SqlParameter("@Code", SqlDbType.NVarChar, 50),
                    new SqlParameter("@Name", SqlDbType.NVarChar, 100),
                    new SqlParameter("@NamePY", SqlDbType.NVarChar, 100),
                    new SqlParameter("@FullName", SqlDbType.NVarChar, 200),
                    new SqlParameter("@Address", SqlDbType.NVarChar, 200),
                    new SqlParameter("@Email", SqlDbType.NVarChar, 100),
                    new SqlParameter("@PostCode", SqlDbType.NVarChar, 50),
                    new SqlParameter("@Fax", SqlDbType.NVarChar, 50),
                    new SqlParameter("@Linkman", SqlDbType.NVarChar, 50),
                    new SqlParameter("@LinkPhone", SqlDbType.NVarChar, 50),
                    new SqlParameter("@Linkman1", SqlDbType.NVarChar, 50),
                    new SqlParameter("@LinkPhone1", SqlDbType.NVarChar, 50),
                    new SqlParameter("@Leveal", SqlDbType.Int, 4),
                    new SqlParameter("@Remark", SqlDbType.NVarChar, 500),
                    new SqlParameter("@IsStop", SqlDbType.Int, 4),
                    new SqlParameter("@AreaID", SqlDbType.Int, 4) };
            parameters[0].Value = model.Code;
            parameters[1].Value = model.Name;
            parameters[2].Value = model.NamePY;
            parameters[3].Value = model.FullName;
            parameters[4].Value = model.Address;
            parameters[5].Value = model.Email;
            parameters[6].Value = model.PostCode;
            parameters[7].Value = model.Fax;
            parameters[8].Value = model.Linkman;
            parameters[9].Value = model.LinkPhone;
            parameters[10].Value = model.Linkman1;
            parameters[11].Value = model.LinkPhone1;
            parameters[12].Value = model.Leveal;
            parameters[13].Value = model.Remark;
            parameters[14].Value = model.IsStop;
            parameters[15].Value = model.AreaID;

            var obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(DMZS.Model.Supplier model)
        {
            var strSql = new StringBuilder();
            strSql.Append("update Supplier set ");
            strSql.Append("Code=@Code,");
            strSql.Append("Name=@Name,");
            strSql.Append("NamePY=@NamePY,");
            strSql.Append("FullName=@FullName,");
            strSql.Append("Address=@Address,");
            strSql.Append("Email=@Email,");
            strSql.Append("PostCode=@PostCode,");
            strSql.Append("Fax=@Fax,");
            strSql.Append("Linkman=@Linkman,");
            strSql.Append("LinkPhone=@LinkPhone,");
            strSql.Append("Linkman1=@Linkman1,");
            strSql.Append("LinkPhone1=@LinkPhone1,");
            strSql.Append("Leveal=@Leveal,");
            strSql.Append("Remark=@Remark,");
            strSql.Append("IsStop=@IsStop,");
            strSql.Append("AreaID=@AreaID");
            strSql.Append(" where ID=@ID");
            var parameters = new SqlParameter[] { new SqlParameter("@Code", SqlDbType.NVarChar, 50),
                    new SqlParameter("@Name", SqlDbType.NVarChar, 100),
                    new SqlParameter("@NamePY", SqlDbType.NVarChar, 100),
                    new SqlParameter("@FullName", SqlDbType.NVarChar, 200),
                    new SqlParameter("@Address", SqlDbType.NVarChar, 200),
                    new SqlParameter("@Email", SqlDbType.NVarChar, 100),
                    new SqlParameter("@PostCode", SqlDbType.NVarChar, 50),
                    new SqlParameter("@Fax", SqlDbType.NVarChar, 50),
                    new SqlParameter("@Linkman", SqlDbType.NVarChar, 50),
                    new SqlParameter("@LinkPhone", SqlDbType.NVarChar, 50),
                    new SqlParameter("@Linkman1", SqlDbType.NVarChar, 50),
                    new SqlParameter("@LinkPhone1", SqlDbType.NVarChar, 50),
                    new SqlParameter("@Leveal", SqlDbType.Int, 4),
                    new SqlParameter("@Remark", SqlDbType.NVarChar, 500),
                    new SqlParameter("@IsStop", SqlDbType.Int, 4),
                    new SqlParameter("@AreaID", SqlDbType.Int, 4),
                    new SqlParameter("@ID", SqlDbType.Int, 4) };
            parameters[0].Value = model.Code;
            parameters[1].Value = model.Name;
            parameters[2].Value = model.NamePY;
            parameters[3].Value = model.FullName;
            parameters[4].Value = model.Address;
            parameters[5].Value = model.Email;
            parameters[6].Value = model.PostCode;
            parameters[7].Value = model.Fax;
            parameters[8].Value = model.Linkman;
            parameters[9].Value = model.LinkPhone;
            parameters[10].Value = model.Linkman1;
            parameters[11].Value = model.LinkPhone1;
            parameters[12].Value = model.Leveal;
            parameters[13].Value = model.Remark;
            parameters[14].Value = model.IsStop;
            parameters[15].Value = model.AreaID;
            parameters[16].Value = model.ID;

            var rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ID)
        {
            var strSql = new StringBuilder();
            strSql.Append("delete from Supplier ");
            strSql.Append(" where ID=@ID");
            var parameters = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4)
            };
            parameters[0].Value = ID;

            var rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string IDlist )
        {
            var strSql = new StringBuilder();
            strSql.Append("delete from Supplier ");
            strSql.Append(" where ID in (" + IDlist + ")  ");
            var rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DMZS.Model.Supplier GetModel(int ID)
        {
            var strSql = new StringBuilder();
            strSql.Append("select  top 1 ID,Code,Name,NamePY,FullName,Address,Email,PostCode,Fax,Linkman,LinkPhone,Linkman1,LinkPhone1,Leveal,Remark,IsStop,AreaID from Supplier ");
            strSql.Append(" where ID=@ID");
            var parameters = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4)
            };
            parameters[0].Value = ID;
            var ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DMZS.Model.Supplier DataRowToModel(DataRow row)
        {
            var model = new DMZS.Model.Supplier();
            if (row != null)
            {
                if (row["ID"] != null && row["ID"].ToString() != string.Empty)
                {
                    model.ID = int.Parse(row["ID"].ToString());
                }
                if (row["Code"] != null)
                {
                    model.Code = row["Code"].ToString();
                }
                if (row["Name"] != null)
                {
                    model.Name = row["Name"].ToString();
                }
                if (row["NamePY"] != null)
                {
                    model.NamePY = row["NamePY"].ToString();
                }
                if (row["FullName"] != null)
                {
                    model.FullName = row["FullName"].ToString();
                }
                if (row["Address"] != null)
                {
                    model.Address = row["Address"].ToString();
                }
                if (row["Email"] != null)
                {
                    model.Email = row["Email"].ToString();
                }
                if (row["PostCode"] != null)
                {
                    model.PostCode = row["PostCode"].ToString();
                }
                if (row["Fax"] != null)
                {
                    model.Fax = row["Fax"].ToString();
                }
                if (row["Linkman"] != null)
                {
                    model.Linkman = row["Linkman"].ToString();
                }
                if (row["LinkPhone"] != null)
                {
                    model.LinkPhone = row["LinkPhone"].ToString();
                }
                if (row["Linkman1"] != null)
                {
                    model.Linkman1 = row["Linkman1"].ToString();
                }
                if (row["LinkPhone1"] != null)
                {
                    model.LinkPhone1 = row["LinkPhone1"].ToString();
                }
                if (row["Leveal"] != null && row["Leveal"].ToString() != string.Empty)
                {
                    model.Leveal = int.Parse(row["Leveal"].ToString());
                }
                if (row["Remark"] != null)
                {
                    model.Remark = row["Remark"].ToString();
                }
                if (row["IsStop"] != null && row["IsStop"].ToString() != string.Empty)
                {
                    model.IsStop = int.Parse(row["IsStop"].ToString());
                }
                if (row["AreaID"] != null && row["AreaID"].ToString() != string.Empty)
                {
                    model.AreaID = int.Parse(row["AreaID"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            var strSql = new StringBuilder();
            strSql.Append("select ID,Code,Name,NamePY,FullName,Address,Email,PostCode,Fax,Linkman,LinkPhone,Linkman1,LinkPhone1,Leveal,Remark,IsStop,AreaID ");
            strSql.Append(" FROM Supplier ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            var strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" ID,Code,Name,NamePY,FullName,Address,Email,PostCode,Fax,Linkman,LinkPhone,Linkman1,LinkPhone1,Leveal,Remark,IsStop,AreaID ");
            strSql.Append(" FROM Supplier ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            var strSql = new StringBuilder();
            strSql.Append("select count(1) FROM Supplier ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            var obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            var strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby );
            }
            else
            {
                strSql.Append("order by T.ID desc");
            }
            strSql.Append(")AS Row, T.*  from Supplier T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }


        /// <summary>
        /// 获得数据列表 扩展
        /// </summary>
        public DataSet GetListForUIExpand(string strWhere)
        {
            var strSql = @"
select c.ID,c.Code,c.Name,c.NamePY,c.FullName,c.Address,c.Email,c.PostCode,c.Fax,c.Linkman,c.LinkPhone,c.Linkman1,c.LinkPhone1,c.Leveal,c.Remark,c.IsStop,c.AreaID,
	a.Code AreaCode, a.Name AreaName
from dbo.Supplier c
left join Area a on a.ID=c.AreaID
";
            if (strWhere.Trim() != string.Empty)
            {
                strSql += " where " + strWhere;
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        public DataRow GetRecordByID(int ID)
        {
            var strSql = @"
select c.ID,c.Code,c.Name,c.NamePY,c.FullName,c.Address,c.Email,c.PostCode,c.Fax,c.Linkman,c.LinkPhone,c.Linkman1,c.LinkPhone1,c.Leveal,c.Remark,c.IsStop,c.AreaID,
	a.Code AreaCode, a.Name AreaName
from dbo.Supplier c
left join Area a on a.ID=c.AreaID
where c.ID=" + ID.ToString();

            var ds = DbHelperSQL.Query(strSql);
            if (ds == null || ds.Tables[0].Rows.Count == 0)
            {
                return null;
            }
            else
            {
                return ds.Tables[0].Rows[0];
            }
        }

        /// <summary>
        /// 添加记录
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int AddExpand(DMZS.Model.Supplier model)
        {
            var strSql = @"
declare @areaCount int, @codeCount int, @unitCount int, @unitCount1 int
select @codeCount=count(1) from Supplier where Code=@Code
if @codeCount>0 begin
    select -1001    --编号重复
    return
end

select @areaCount=count(1) from Area where ID=@AreaID
if @areaCount=0 begin
    select -1002    --区域不存在
    return
end

insert into Supplier(Code,Name,NamePY,FullName,Address,Email,PostCode,Fax,Linkman,LinkPhone,Linkman1,LinkPhone1,Leveal,Remark,IsStop,AreaID)
values (@Code,@Name,@NamePY,@FullName,@Address,@Email,@PostCode,@Fax,@Linkman,@LinkPhone,@Linkman1,@LinkPhone1,@Leveal,@Remark,@IsStop,@AreaID)
select @@IDENTITY";

            var parameters = new SqlParameter[] { new SqlParameter("@Code", SqlDbType.NVarChar, 50),
                    new SqlParameter("@Name", SqlDbType.NVarChar, 100),
                    new SqlParameter("@NamePY", SqlDbType.NVarChar, 100),
                    new SqlParameter("@FullName", SqlDbType.NVarChar, 200),
                    new SqlParameter("@Address", SqlDbType.NVarChar, 200),
                    new SqlParameter("@Email", SqlDbType.NVarChar, 100),
                    new SqlParameter("@PostCode", SqlDbType.NVarChar, 50),
                    new SqlParameter("@Fax", SqlDbType.NVarChar, 50),
                    new SqlParameter("@Linkman", SqlDbType.NVarChar, 50),
                    new SqlParameter("@LinkPhone", SqlDbType.NVarChar, 50),
                    new SqlParameter("@Linkman1", SqlDbType.NVarChar, 50),
                    new SqlParameter("@LinkPhone1", SqlDbType.NVarChar, 50),
                    new SqlParameter("@Leveal", SqlDbType.Int, 4),
                    new SqlParameter("@Remark", SqlDbType.NVarChar, 500),
                    new SqlParameter("@IsStop", SqlDbType.Int, 4),
                    new SqlParameter("@AreaID", SqlDbType.Int, 4) };
            parameters[0].Value = model.Code;
            parameters[1].Value = model.Name;
            parameters[2].Value = model.NamePY;
            parameters[3].Value = model.FullName;
            parameters[4].Value = model.Address;
            parameters[5].Value = model.Email;
            parameters[6].Value = model.PostCode;
            parameters[7].Value = model.Fax;
            parameters[8].Value = model.Linkman;
            parameters[9].Value = model.LinkPhone;
            parameters[10].Value = model.Linkman1;
            parameters[11].Value = model.LinkPhone1;
            parameters[12].Value = 0;
            parameters[13].Value = model.Remark;
            parameters[14].Value = model.IsStop;
            parameters[15].Value = model.AreaID;

            var obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        public int UpdateExpand(DMZS.Model.Supplier model)
        {
            var strSql = @"
declare @areaCount int, @codeCount int, @unitCount int, @unitCount1 int
select @codeCount=count(1) from Supplier where Code=@Code and ID<>@ID
if @codeCount>0 begin
    select -1001    --编号重复
    return
end

select @areaCount=count(1) from Area where ID=@AreaID
if @areaCount=0 begin
    select -1002    --区域不存在
    return
end

declare @recordCount int

select @recordCount=count(1) from Client where ID=@ID
if @recordCount=0 begin
    select 0 
    return
end

update Supplier set 
    Code=@Code,
    Name=@Name,
    NamePY=@NamePY,
    FullName=@FullName,
    Address=@Address,
    Email=@Email,
    PostCode=@PostCode,
    Fax=@Fax,
    Linkman=@Linkman,
    LinkPhone=@LinkPhone,
    Linkman1=@Linkman1,
    LinkPhone1=@LinkPhone1,
    Leveal=@Leveal,
    Remark=@Remark,
    IsStop=@IsStop,
    AreaID=@AreaID
where ID=@ID
select @ID
";
            var parameters = new SqlParameter[] { new SqlParameter("@Code", SqlDbType.NVarChar, 50),
                    new SqlParameter("@Name", SqlDbType.NVarChar, 100),
                    new SqlParameter("@NamePY", SqlDbType.NVarChar, 100),
                    new SqlParameter("@FullName", SqlDbType.NVarChar, 200),
                    new SqlParameter("@Address", SqlDbType.NVarChar, 200),
                    new SqlParameter("@Email", SqlDbType.NVarChar, 100),
                    new SqlParameter("@PostCode", SqlDbType.NVarChar, 50),
                    new SqlParameter("@Fax", SqlDbType.NVarChar, 50),
                    new SqlParameter("@Linkman", SqlDbType.NVarChar, 50),
                    new SqlParameter("@LinkPhone", SqlDbType.NVarChar, 50),
                    new SqlParameter("@Linkman1", SqlDbType.NVarChar, 50),
                    new SqlParameter("@LinkPhone1", SqlDbType.NVarChar, 50),
                    new SqlParameter("@Leveal", SqlDbType.Int, 4),
                    new SqlParameter("@Remark", SqlDbType.NVarChar, 500),
                    new SqlParameter("@IsStop", SqlDbType.Int, 4),
                    new SqlParameter("@AreaID", SqlDbType.Int, 4),
                    new SqlParameter("@ID", SqlDbType.Int, 4) };
            parameters[0].Value = model.Code;
            parameters[1].Value = model.Name;
            parameters[2].Value = model.NamePY;
            parameters[3].Value = model.FullName;
            parameters[4].Value = model.Address;
            parameters[5].Value = model.Email;
            parameters[6].Value = model.PostCode;
            parameters[7].Value = model.Fax;
            parameters[8].Value = model.Linkman;
            parameters[9].Value = model.LinkPhone;
            parameters[10].Value = model.Linkman1;
            parameters[11].Value = model.LinkPhone1;
            parameters[12].Value = 0;
            parameters[13].Value = model.Remark;
            parameters[14].Value = model.IsStop;
            parameters[15].Value = model.AreaID;
            parameters[16].Value = model.ID;

            var obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return -1000;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 获取行记录
        /// </summary>
        /// <param name="Code"></param>
        /// <returns></returns>
        public DataRow GetRecordByCode(string Code)
        {
            var strSql = @"
select c.ID,c.Code,c.Name,c.NamePY,c.FullName,c.Address,c.Email,c.PostCode,c.Fax,c.Linkman,c.LinkPhone,c.Linkman1,c.LinkPhone1,c.Leveal,c.Remark,c.IsStop,c.AreaID,
	a.Code AreaCode, a.Name AreaName
from dbo.Supplier c
left join Area a on a.ID=c.AreaID
where c.Code=@Code";
            var parameters = new SqlParameter[] { new SqlParameter("@Code", SqlDbType.NVarChar, 50)
            };
            parameters[0].Value = Code;

            var ds = DbHelperSQL.Query(strSql, parameters);
            if (ds == null || ds.Tables[0].Rows.Count == 0)
            {
                return null;
            }
            else
            {
                return ds.Tables[0].Rows[0];
            }
        }

        public DataSet GetListForDialog(string Code, string Name)
        {
            var strSql = @"
select c.ID,c.Code,c.Name,c.NamePY,c.FullName,c.Address,c.Email,c.PostCode,c.Fax,c.Linkman,c.LinkPhone,c.Linkman1,c.LinkPhone1,c.Leveal,c.Remark,c.IsStop,c.AreaID,
	a.Code AreaCode, a.Name AreaName
from dbo.Supplier c
left join Area a on a.ID=c.AreaID
where c.Code like '%'+@Code+'%' and c.Name like '%'+@Name+'%' ";

            var parameters = new SqlParameter[] { 
                new SqlParameter("@Code", SqlDbType.NVarChar, 50),
                new SqlParameter("@Name", SqlDbType.NVarChar, 100)};
            parameters[0].Value = Code;
            parameters[1].Value = Name;

            return DbHelperSQL.Query(strSql.ToString(), parameters);
        }
    }
}

